﻿#region Copyrigth and License
/*==============================================================================
 *  Copyright (c) cndotnet.org Corporation.  All rights reserved.
 * ===============================================================================
 * This code and information is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the implied warranties
 * of merchantability and fitness for a particular purpose.
 * ===============================================================================
 * Licensed under the GNU General Public License (GPL) v2
 * http://www.cndotnet.org/ezsocio
 * ==============================================================================*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Collections.Specialized;
using System.Diagnostics;
using DusonFramework.Core;

namespace DusonFramework.ContactService.Email {
    public  class EmailSinaService:IEmailService {
        #region Fields

        #endregion

        #region Constructors

        #endregion

        #region Properties
        public string EmailSystem {
            get { return "sina.com.cn"; }
        }

        public string StartUrl {
            get { return "http://mail.sina.com.cn/cgi-bin/login.cgi"; }
        }

        public Encoding DefaultEncoding {
            get { return Encoding.GetEncoding("utf-8"); }
        }
        #endregion

        #region Events

        #endregion
       

        public MailContractCollection GetContracts(string userName, string pwd) {
            MailContractCollection mc = new MailContractCollection();
            string returnUrl = StartUrl;
            CookieContainer cc = new CookieContainer();
            string accept = "image/gif, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-ms-xbap, application/x-silverlight-2-b2, application/x-silverlight, */*";
            string language = "zh-cn";
            string refer = "http://mail.sina.com.cn/";
            string contentType = "application/x-www-form-urlencoded";
            string html = WebOpHelper.GetHtml("http://mail.sina.com.cn/", accept, contentType, refer, language, cc, DefaultEncoding, out returnUrl);
            NameValueCollection nvc = new NameValueCollection();
            nvc.Add("btnloginfree", "登陆");
            nvc.Add("logintype", "uid");
            nvc.Add("psw", pwd);
            nvc.Add("u", userName);            
            string postContent = WebOpHelper.GetNameValueRowData(nvc, '&');
           
            string startPageHTMl = WebOpHelper.Post(StartUrl, accept, contentType, refer,
                language, postContent, cc, Encoding.GetEncoding("utf-8"), out returnUrl);
            Uri u = new Uri(returnUrl);           
            string url = "http://"+u.Host+"/classic/addr_member.php";           
            postContent = "act=list&sort_item=letter&sort_type=desc";        
            refer = returnUrl;               
            string cks = cc.GetCookieHeader(new Uri(url));
            Debug.WriteLine(cks);
            html = WebOpHelper.Post(url, accept, contentType, refer,
                language, postContent, ref cc, Encoding.GetEncoding("gb2312"), out returnUrl);

            var jsonObj = JsonFormaterUtils.Deserialize<SinaJson>(html, Encoding.GetEncoding("gb2312"));
            
            if (jsonObj.data.totalMem == 1&&jsonObj.data.contact!=null) {
                foreach (var sm in jsonObj.data.contact) {
                    MailContract m = new MailContract();
                    m.Email = sm.email;
                    m.Name = m.Email;
                    if(!string.IsNullOrEmpty(sm.name)){
                        m.Name = sm.name;
                    }
                    mc.Add(m);
                }
            }
            return mc;
        }
    }
}
